import "package:ct_pa_majordomo/config/roomIcons.dart";
import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/device/lock/api.dart";
import "package:ct_pa_majordomo/pages/device/lock/detail/lock_detail_notifier.dart";
import "package:ct_pa_majordomo/pages/device/lock/electronic_key/index.dart";
import "package:ct_pa_majordomo/pages/device/lock/fingerprint/index.dart";
import "package:ct_pa_majordomo/pages/device/lock/password_key/index.dart";
import "package:ct_pa_majordomo/pages/device/lock/setting/index.dart";
import "package:ct_pa_majordomo/pages/device/lock/widgets/operation_record/index.dart";
import "package:ct_pa_majordomo/router/keys.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:ct_pa_majordomo/widgets/wrap_item.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";

class Config {
  final String name;
  final String path;
  final Object? arguments;
  final Widget icon;
  Config(this.name, this.path, this.arguments, this.icon);
}

class Operation extends StatefulWidget {
  final LockDetailNotifier controller;
  const Operation({super.key, required this.controller});

  @override
  State<Operation> createState() => _OperationState();

}

class _OperationState extends State<Operation> {
  late final LockDetailNotifier _controller = widget.controller;

  Map _info = {};
  List<Config> _config = [];

  @override
  void initState() {
    _controller.addListener(() {
      if (mounted) {
        setState(() {
          _info = _controller.info;
        });
        setState(() {
          _config = [
            Config("电子钥匙", Keys.DEVICE_LOCK_ELECTRONICKEY, int.parse(_info["lockId"]), const Icon(RoomIcons.electricity_key, size: 40, color: ThemeVars.colorPrimary,),),
            Config("密码", Keys.DEVICE_LOCK_PASSWORDKEY, _info, const Icon(RoomIcons.password_key, size: 40, color: ThemeVars.colorPrimary,),),
            Config("IC卡", Keys.DEVICE_LOCK_IC_CARD, _info, const Icon(Icons.credit_card, size: 40, color: ThemeVars.colorPrimary,),),
            Config("指纹", Keys.DEVICE_LOCK_FINGERPRINT, _info, const Icon(Icons.fingerprint, size: 40, color: ThemeVars.colorPrimary,),),
            Config("操作记录", Keys.DEVICE_LOCK_OPERATION, {"lockId": int.parse(_info["lockId"]), "lockData": _info["lockData"]}, const Icon(RoomIcons.operation_record, size: 40, color: ThemeVars.colorPrimary,),),
            Config("设置", Keys.DEVICE_LOCK_SETTING, _info, const Icon(Icons.settings, size: 40, color: ThemeVars.colorPrimary,),)
          ];
        });
      }
    });
    super.initState();
  }

  // @override
  // void dispose() {
  //   _controller.dispose();
  //   super.dispose();
  // }

  @override
  Widget build(BuildContext context) {
    print("西悉尼：$_info");

    return DecoratedBox(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(12),
          boxShadow: const [ThemeVars.boxShadow],
          color: Colors.white,
        ),
        child: Container(
          padding: const EdgeInsets.symmetric(vertical: 16),
          child: Wrap(
            children: _config.map((ele) {
              return GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  Stacker.pushNamed(ele.path, arguments: ele.arguments);
                  // Navigator.push(context, CupertinoPageRoute(
                  //   settings: ele.name == "指纹" ? const RouteSettings(name: "fingerprint") : null,
                  //   builder: (BuildContext context) {
                  //     return ele.pages;
                  //   }
                  // ));
                },
                child: WrapItem(
                    span: 4,
                    child: Container(
                      padding: const EdgeInsets.symmetric(vertical: 16),
                      child: Center(
                        child: Column(
                          children: [
                            SizedBox(
                              width: 40,
                              height: 40,
                              child: ele.icon,
                            ),
                            const SizedBox(height: 10,),
                            Text(ele.name, style: const TextStyle(fontWeight: FontWeight.w600),),
                          ],
                        ),
                      ),
                    )
                ),
              );
            }).toList(),
          ),
        )
    );
  }

}